package com.example.talkheart.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import com.example.talkheart.MyApplication
import com.example.talkheart.R
import com.example.talkheart.database.ScoreDatabase
import com.example.talkheart.entity.TeacherBean

open class TeacherAdapter: RecyclerView.Adapter<TeacherAdapter.TeacherViewHolder>() {
    private var dataList : List<TeacherBean> = listOf()
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): TeacherViewHolder {
        val view = LayoutInflater.from(MyApplication.context).inflate(R.layout.teacher_item,parent,false)
        return TeacherViewHolder(view)
    }

    override fun onBindViewHolder(holder: TeacherViewHolder, position: Int) {
        holder.teacherName.text = dataList.get(position).name
        holder.helpTime.text = dataList.get(position).time
    }

    override fun getItemCount(): Int {
        return if(dataList.isEmpty())
            0
        else
            dataList.size
    }

    open fun setDataList(){
        dataList = ScoreDatabase.getScoreInstance(MyApplication.context).teacherDatabaseDao.queryAll()
        if(dataList.isEmpty()){
            ScoreDatabase.getScoreInstance(MyApplication.context).teacherDatabaseDao.insert(TeacherBean(1,"李华老师","4月14日16:00-18:00"))
            ScoreDatabase.getScoreInstance(MyApplication.context).teacherDatabaseDao.insert(TeacherBean(2,"李芳老师","4月14日12:00-13:00"))
            ScoreDatabase.getScoreInstance(MyApplication.context).teacherDatabaseDao.insert(TeacherBean(3,"李明老师","4月14日17:00-19:00"))
            dataList = ScoreDatabase.getScoreInstance(MyApplication.context).teacherDatabaseDao.queryAll()
        }
    }

    open fun refresh(){
        notifyItemRangeChanged(0,dataList.size)
    }

    class TeacherViewHolder(itemView : View): RecyclerView.ViewHolder(itemView){
        val teacherName : TextView = itemView.findViewById(R.id.teacher_name)
        val helpTime : TextView = itemView.findViewById(R.id.help_time)
    }
}